

import UIKit


final class HLTContolButton: UIControl {
    var clickBtn:(HLTContolButton) -> () = { btn in
 
     }

    var intrinsicContentExtraSize = CGSize.zero // 距离两侧的间距
    
    var lableFont:UIFont?{
        didSet{
            self.lable.font = self.lableFont
        }
    }
    var lableTextColor:UIColor?{
        didSet{
            self.lable.textColor = self.lableTextColor
        }
    }
    
    var spacing:CGFloat = .zero {
        didSet{
            self.stackView.spacing = self.spacing
            self.invalidateIntrinsicContentSize()
        }
    }
    var iconSize:CGSize = .zero{
        didSet{
            self.imagView.widthAnchor.constraint(equalToConstant: self.iconSize.width).isActive = true
              
            self.imagView.heightAnchor.constraint(equalToConstant: self.iconSize.height).isActive = true
        }
    }
    var lableSize:CGSize = .zero{
        didSet{
            self.lable.widthAnchor.constraint(equalToConstant: self.lableSize.width).isActive = true
              
            self.lable.heightAnchor.constraint(equalToConstant: self.lableSize.height).isActive = true
        }
    }
    
  
    
    private var lable = UILabel()
    private var imagView = UIImageView()
    private var stackView = UIStackView()
    private var selectedText = ""
    private var normalText = ""
    private var selectedTitleColor:UIColor?
    private var normalTitleColor:UIColor?
    private var selectedImage:UIImage?
    private var normalImage:UIImage?

//    // 文字在左 图片在右。
   class func layoutLeadingtitle(title:String,imageName:String,axi:NSLayoutConstraint.Axis) -> HLTContolButton{
        let btn = HLTContolButton ()
        btn.lable.text = title
        btn.normalText = title
        btn.selectedText = title
        btn.imagView.image = UIImage.init(named: imageName)
        btn.selectedImage = UIImage.init(named: imageName)
        btn.normalImage = UIImage.init(named: imageName)
        btn.stackView.addArrangedSubview(btn.lable)
        btn.stackView.addArrangedSubview(btn.imagView)
        btn.stackView.axis = axi
        return btn
    }

    
    // 图片在左文字在右
    class  func layoutLeadingimageName(imageName:String,title:String,axi:NSLayoutConstraint.Axis) -> HLTContolButton {
        let btn = HLTContolButton()
        btn.lable.text = title
        btn.normalText = title
        btn.selectedText = title
        btn.imagView.image = UIImage.init(named: imageName)
        btn.selectedImage = UIImage.init(named: imageName)
        btn.normalImage = UIImage.init(named: imageName)
        btn.stackView.addArrangedSubview(btn.imagView)
        btn.stackView.addArrangedSubview(btn.lable)
        btn.stackView.axis = axi
        return btn
    }
    
 @objc   func setTitle(title:String,state:UIControl.State) {
        if state == .selected {
            self.selectedText = title
            self.lable.text = self.selectedText
        }else {
            self.normalText = title
            self.lable.text = self.normalText
        }
        
        if (self.isSelected == true) {
            self.lable.text = self.selectedText
        }else{
            self.lable.text = self.normalText
        }
     
        self.invalidateIntrinsicContentSize()
    }
    
    func setTitleColor(color:UIColor,state:UIControl.State) {
        if (state == .selected) {
            self.selectedTitleColor = color
            self.lable.textColor = self.selectedTitleColor
        }else{
            self.normalTitleColor = color
            self.lable.textColor = self.normalTitleColor
        }
        if (self.isSelected == true) {
            self.lable.textColor = self.selectedTitleColor
        }else{
            self.lable.textColor = self.normalTitleColor
        }
    }
    
    func setImage(imageName:String,state:UIControl.State) {
        if (state == .selected) {
            self.selectedImage = UIImage(named: imageName)
        }else{
            self.normalImage = UIImage(named: imageName)
        }
        
        if (self.isSelected == true) {
            self.imagView.image = self.selectedImage
        }else{
            self.imagView.image = self.normalImage
        }
        self.invalidateIntrinsicContentSize()
    }
    func setImage(image:UIImage,state:UIControl.State) {
        if (state == .selected) {
            self.selectedImage = image;
        }else{
            self.normalImage = image;
        }
        if (self.isSelected == true) {
            self.imagView.image = self.selectedImage;
        }else{
            self.imagView.image = self.normalImage;
        }
        self.invalidateIntrinsicContentSize()
     
    }
    
    override var isSelected: Bool {
        didSet {
            super.isSelected = isSelected
            if (isSelected == true) {
                self.lable.text = self.selectedText
                self.lable.textColor = self.selectedTitleColor
                self.imagView.image = self.selectedImage
            }else{
                self.lable.text = self.normalText
                self.lable.textColor = self.normalTitleColor
                self.imagView.image = self.normalImage
            }
         
            self.invalidateIntrinsicContentSize()

        }
    }
    
    
    override var intrinsicContentSize: CGSize{
        let size = self.stackView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
        let w = size.width+self.intrinsicContentExtraSize.width
        let h = size.height+self.intrinsicContentExtraSize.height
        return CGSize(width: w, height: h)
    }
//    
//    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//        super.touchesBegan(touches, with: event)
//       
//            self.clickBtn(self)
//        
//    }
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
      let view =  super.hitTest(point, with: event)
       return view
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let stackview = UIStackView()
        self.addSubview(stackview)
        stackview.isUserInteractionEnabled = false
        stackview.translatesAutoresizingMaskIntoConstraints = false
        stackview.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        stackview.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        stackview.alignment = .center

        self.lable = UILabel()
        self.lable.isUserInteractionEnabled = false
        self.normalTitleColor = self.lable.textColor
        self.selectedTitleColor = self.lable.textColor
        self.imagView.isUserInteractionEnabled = false
        self.imagView = UIImageView()
        self.stackView = stackview
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

}
